<?php
/** [管理员控制器]
 * @Author: 976123967@qq.com
 * @Date:   2015-01-16 11:33:22
 * @Last Modified by:   chenli
 * @Last Modified time: 2015-03-05 23:39:02
 */

class AdminAction extends PublicAction{


	public $logic;
	/**
	 * [_initialize 初始化]
	 * @return [type] [description]
	 */
	public function _initialize()
	{
		parent::_initialize();
		$this->logic = D('User','Logic');
	}


	/**
	 * [index 管理员列表]
	 * @return [type] [description]
	 */
	public function index()
	{
		// 读取管理员列表 1管理员 2 普通会员
		$data = $this->logic->get_all(array(1));

		if($data)
		{
			$this->assign('page',$data['page']);
			unset($data['page']);
		}
		$this->assign('data',$data);
		$this->display();
	}

	/**
	 * [add 添加]
	 */
	public function add()
	{
		if(IS_POST)
		{
			if(!$uid = $this->logic->add_user())
				$this->error($this->logic->getError());
			D('AuthGroupAccess','Logic')->alter_auth_group_access($uid);
			$this->success('管理员添加成功',U('Admin/index'));
			die;
		}

		$group = D('AuthGroup')->where(array('status'=>1))->select();
		$this->assign('group',$group);

		$this->display();
	}


	/**
	 * [edit 编辑]
	 * @return [type] [description]
	 */
	public function edit()
	{
		if(IS_POST)
		{
			if(!$this->logic->update_user())
				$this->error($this->logic->getError());
			D('AuthGroupAccess','Logic')->alter_auth_group_access(I('post.uid'));
			$this->success('管理员修改成功',U('Admin/index'));
			die;
		}
		$field = $this->logic->find_one();
		$this->assign('field',$field);

		$group = D('AuthGroup')->where(array('status'=>1))->select();
		$this->assign('group',$group);

		// 原先用户组信息
		$userGroup = D('AuthGroupAccessView')->where(array('uid'=>I('get.uid')))->select();
		$this->assign('userGroup',$userGroup);
		$this->display();
	}

	/**
	 * [del 删除]
	 * @return [type] [description]
	 */
	public function del()
	{	
		if(!$this->logic->delete_user())
			$this->error($this->logic->getError());
		D('AuthGroupAccess',"Logic")->delete_auth_group_access();
		$this->success('管理员删除成功',U('Admin/index'));
	}


	/**
	 * [info 修改会员信息]
	 * @return [type] [description]
	 */
	public function info()
	{

		if(IS_POST)
		{
			if(!$this->logic->update_cur())
				$this->error($this->logic->getError());
			$this->success('修改成功',U('Admin/info'));
			die;
		}
		$field = $this->logic->find_cur();
		$this->assign('field',$field);
		$this->display();	
	}

	/**
	 * [change 修改会员密码]
	 * @return [type] [description]
	 */
	public function change()
	{

		if(IS_POST)
		{
			if(!$this->logic->update_change())
				$this->error($this->logic->getError());
			$this->success('修改密码成功',U('Admin/change'));
			die;
		}
		$field = $this->logic->find_cur();
		$this->assign('field',$field);
		$this->display();	
	}

	/**
	 * [operation 批量操作数据]
	 * @return [type] [description]
	 */
	public function operation()
	{
		if(!$msg = $this->logic->operation())
			$this->error($this->logic->getError());
		$this->success($msg,U('Admin/index'));
	}

}